package coffeebar;

import javax.swing.*;

import java.awt.*;
import java.util.*;
import java.awt.event.*;

public class Grid extends JPanel {
	Hashtable<GridObject, JButton> buttons;

	public Grid(int width, int height) {
		buttons = new Hashtable<GridObject, JButton>();

		this.setLayout(new GridLayout(width, height));
	}

	public void addItem(GridObject g) {
		JButton button = new JButton();
		// set various properties coming from the item object
		buttons.put(g, button);
		button.setText(g.getName());
		button.setIcon(g.getImage());
		button.setVerticalTextPosition(AbstractButton.BOTTOM);
		button.setHorizontalAlignment(AbstractButton.CENTER);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JButton b = (JButton) e.getSource();
				Iterator it = buttons.keySet().iterator();
				while (it.hasNext()) {
					GridObject g = (GridObject) it.next();
					if ((JButton) buttons.get(g) == b) {
						g.event();
					}
				}
			}
		});
		this.add(button);
	}

	public void removeItem(Item item) {
		JButton button = (JButton) buttons.get(item);
		this.remove(button);
	}
}
